package org.eclipse.rally.plugin.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.rally.plugin.webservice.RallyMain;
import org.eclipse.swt.custom.CCombo;

import com.rallydev.webservice.v1_12.domain.User;

public class UserUtil {
	public static final List userList = RallyMain.listUsers();
	
	public static Map userMap;

	static {
		userMap = getUserDisplayName(userList);
	}

	public static Map getUserDisplayName(List userList) {
		userMap = new HashMap();

		if ((userList != null) && (userList.size() > 0)) {
			for (int i = 0; i < userList.size(); i++) {
				User user = (User) userList.get(i);

				if (user.getEmailAddress() != null) {
					userMap.put(user.getEmailAddress(), user.getRefObjectName());
				}
			}
		}

		return userMap;
	}

	public static String getUserMailId(CCombo combo) {
		String userMailId = "";

		if ((userList != null) && (userList.size() > 0)) {
			for (int i = 0; i < userList.size(); i++) {
				User user = (User) userList.get(i);

				if (user.getRefObjectName() != null) {
					if (user.getRefObjectName().equalsIgnoreCase(combo.getText())) {
						userMailId = user.getEmailAddress();
					}
				}
			}
		}

		return userMailId;
	}

	public static void setUsers(CCombo combo) {
		String userName = "";
		List<String> userNameList = new ArrayList<String>();
		// List userList = rallyMain.listUsers();
		System.out.println("size of of the user list in UserUtil" + userList.size());

		if ((userList != null) && (userList.size() > 0)) {
			userNameList.add("All");

			// strValue = new String[tempLst.size()-1];
			for (int i = 0; i < userList.size(); i++) {
				User user = (User) userList.get(i);

				if (user.getRefObjectName() != null) {
					userName = user.getRefObjectName();
					userNameList.add(userName);
				}
			}

			if ((userNameList != null) && (userNameList.size() > 0)) {
				String[] userStr = userNameList.toArray(new String[0]);
				combo.setItems(userStr);
			}
		}
	}

	public static String getUsrName(String emailId) {
		return (String) userMap.get(emailId);
	}

}
